package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.CoopTalkReplyDao;
import com.hhit.collaborative_learning.dao.CoopTaskReplyDao;
import com.hhit.collaborative_learning.dao.impl.CoopTalkReplyDaoImpl;
import com.hhit.collaborative_learning.dao.impl.CoopTaskReplyDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class PutCoopTaskReplyServlet
 */
@WebServlet("/communication/PutCoopTaskReplyServlet")
public class PutCoopTaskReplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PutCoopTaskReplyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String task_id=request.getParameter("task_id");
		String user_id=request.getParameter("user_id");
		String body=request.getParameter("body");
		
		Connection con = DBAccess.getInstance().getConn();
		CoopTaskReplyDao tr=new CoopTaskReplyDaoImpl();
		String isSuccess="false";
		JSONObject jobj = new JSONObject();
		int re=tr.insert(con, body, user_id, task_id);
		if(re!=0){
			isSuccess="true";
		}
		jobj.put("isSuccess", isSuccess);
		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
			System.out.println("put Talk reply json "+jobj.toString());
		}
	}

}
